package com.xdap.keymen.service.dayang;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.keymen.entity.dayang.DaYangOrderProductEntity;
import com.xdap.runtime.service.RuntimeAppContextService;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class DaYangOrderPorductService {
    private final RuntimeDatasourceService runtimeDatasourceService;
    private final RuntimeAppContextService runtimeAppContextService;

    /**
     * 获取订单详情
     * @param endity
     * @return
     */
    public DaYangOrderProductEntity getOrderDetail(DaYangOrderProductEntity endity) {
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        MpaasQuery query = runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId);
        query.eq("tab_doc_id", endity.getTabDocId());
        query.eq("style_no", endity.getStyleNo());
        query.eq("color", endity.getColor());
        if(StringUtils.isNotBlank(endity.getProductSize())){
            query.eq("product_size", endity.getProductSize());
        }
        return query.doQueryFirst(DaYangOrderProductEntity.class);
    }
    public void updOrderProduct(DaYangOrderProductEntity entity) {
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId)
                .table("dy_posno_sub")
                .update("storage_status","1")
                .update("last_update_date", LocalDateTime.now())
                .update("danjuhao_dz",entity.getDanjuhaoDz())
                .eq("id", entity.getId())
                .doUpdate();
    }
}
